#include "transfer_progress_dialog.hpp"
#include <QFileInfo>
#include <QStyle>

// TransferState enum might not be needed if setTransferState is removed or status comes as string
// enum TransferState
// {
//     None = 0, Starting = 1, Active = 2, Paused = 3,
//     Completed = 4, Error = 5, Canceled = 6
// };

TransferProgressDialog::TransferProgressDialog(const QString &title, const QString &fileNameToDisplay, QWidget *parent)
    : QDialog(parent) // m_filePath removed from member init, fileNameToDisplay is for the label
{
    setWindowTitle(title);
    setMinimumWidth(350); // Adjusted min width slightly
    // setMinimumHeight(150); // Height will be determined by content

    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    m_fileNameLabel = new QLabel(tr("文件: %1").arg(fileNameToDisplay), this);
    mainLayout->addWidget(m_fileNameLabel);

    m_progressBar = new QProgressBar(this);
    m_progressBar->setRange(0, 100);
    m_progressBar->setValue(0);
    mainLayout->addWidget(m_progressBar);

    m_statusLabel = new QLabel(tr("准备中..."), this);
    mainLayout->addWidget(m_statusLabel);

    m_cancelButton = new QPushButton(tr("取消"), this);
    mainLayout->addWidget(m_cancelButton, 0, Qt::AlignRight);

    setLayout(mainLayout);

    connect(m_cancelButton, &QPushButton::clicked, this, &TransferProgressDialog::onCancelClicked);

    // Removed m_rateTimer and m_elapsed initialization and connections
}

TransferProgressDialog::~TransferProgressDialog()
{
    // Removed m_rateTimer.stop();
}

void TransferProgressDialog::updateProgress(int percentage, const QString &statusMessage)
{
    m_progressBar->setValue(percentage);
    m_statusLabel->setText(statusMessage.isEmpty() ? tr("进行中... (%1%)").arg(percentage) : statusMessage);
}

// Consider removing setTransferState or simplifying if statusMessage in updateProgress is sufficient.
// void TransferProgressDialog::setTransferState(int state) { ... }

void TransferProgressDialog::markCompleted(const QString &message)
{
    m_progressBar->setValue(100);
    m_statusLabel->setText(message.isEmpty() ? tr("完成!") : message);
    m_cancelButton->setText(tr("关闭"));
    m_cancelButton->setEnabled(true); // Ensure it's enabled to close
    setWindowIcon(style()->standardIcon(QStyle::SP_DialogApplyButton));
}

void TransferProgressDialog::markError(const QString &errorMessage)
{
    m_statusLabel->setText(tr("错误: %1").arg(errorMessage));
    m_progressBar->setValue(0); // Or keep current value, depends on preference for error state
    m_cancelButton->setText(tr("关闭"));
    m_cancelButton->setEnabled(true); // Ensure it's enabled to close
    setWindowIcon(style()->standardIcon(QStyle::SP_MessageBoxCritical));
}

void TransferProgressDialog::onCancelClicked()
{
    if (m_cancelButton->text() == tr("取消"))
    {
        emit cancelRequested();
        m_statusLabel->setText(tr("正在取消..."));
        m_cancelButton->setEnabled(false); // Disable while processing cancellation
    }
    else
    {
        accept(); // Close the dialog if button is "关闭"
    }
}

// Removed updateTransferRate() and formatSize() methods